﻿Imports AForge.Imaging.Filters
Imports AForge
Imports System.Drawing
Imports UCBase

Public Class LightAmp
    Implements I_ImageExchange
    Private WithEvents Inp As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover
    Private SourceBitmap As System.Drawing.Bitmap
    Private OverLay As Bitmap
    Private DestBitmap As System.Drawing.Bitmap
    Private CurrentCount As Integer = 0
    Private Sub MoveToFilter()
        If CheckBox1.Checked Then
            For c = 1 To KnobControl1.Value
                Dim BM As New Bitmap(SourceBitmap)
                Dim BMB As New Bitmap(SourceBitmap)
                Dim filter As Add = New Add(BM)
                DestBitmap = filter.Apply(BMB)
                BM.Dispose()
                BMB.Dispose()
                SourceBitmap = DestBitmap
            Next c
        Else
            Dim filter As Add = New Add(OverLay)
            DestBitmap = filter.Apply(SourceBitmap)
            SourceBitmap = DestBitmap
        End If
    End Sub

    Public Event ImageOut(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height, Color.MediumSlateBlue)
        Mov.Connect(Me)
        KnobControl1.ContainerColor = Backpic.FaceColor
        KnobControl1.Refresh()
    End Sub
    Private Sub Socket1_Connect() Handles Socket1.Connect
        Dim S As Socket = Socket1.Remote
        Inp = S.Parent
    End Sub

    Private Sub UniversalColorFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Width = 255
        Me.Height = 140
    End Sub

    Private Sub Inp_ImageOut(ByVal Image As System.Drawing.Bitmap) Handles Inp.ImageOut1
        If CheckBox1.Checked Then
            SourceBitmap = Image
            MoveToFilter()
            RaiseEvent ImageOut(DestBitmap)
        Else
            If CurrentCount = 0 Then
                SourceBitmap = Image
                'DestBitmap = SourceBitmap
                RaiseEvent ImageOut(DestBitmap)
            Else
                OverLay = Image
                MoveToFilter()
            End If
            CurrentCount = CurrentCount + 1
            If CurrentCount > KnobControl1.Value Then CurrentCount = 0
            ProgressBar1.Value = CurrentCount
        End If
    End Sub

    Private Sub KnobControl1_ValueChanged(ByVal Sender As Object) Handles KnobControl1.ValueChanged
        ProgressBar1.Maximum = KnobControl1.Value
        CurrentCount = 0
        ProgressBar1.Value = 0
    End Sub

    Private Sub Socket1_Disconnected() Handles Socket1.Disconnected
        Inp = Nothing
    End Sub
    Private Sub Socket1_EnableStateChanged(ByVal State As Boolean) Handles Socket1.EnableStateChanged
        Socket2.SocketsEnable = Socket1.SocketsEnable
        If Socket1.SocketsEnable = False Then
            CurrentCount = 0
            ProgressBar1.Value = 0
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    End Sub
End Class
